package com.fy.fyai.controller;

import com.fy.fyai.common.config.AiModelManager;
import com.fy.fyai.common.constant.PromptConstant;
import com.fy.fyai.common.enums.AiModelEnum;
import com.fy.fyai.common.result.R;
import com.fy.fyai.service.RagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.fy.fyai.common.result.R.ok;

/**
 * @author fwq
 * @description RAG控制
 * @date 2025-10-26 11:06
 */
@Tag(name = "RAG相关接口")
@Slf4j
@RestController
@RequestMapping("/rag")
public class RagController {

    @Resource
    private RagService ragService;

    /**
     * 上传RAG文件
     *
     * @param ragTag
     * @param files
     * @return 响应结果
     */
    @Operation(summary = "上传RAG文件", description = "上传知识库文件, ragTag为区分知识库标签")
    @PostMapping(value = "/uploadRagFile")
    public R uploadFile(@RequestParam("ragTag") String ragTag, @RequestParam("files") MultipartFile files) {
        List<MultipartFile> list = List.of(files);
        return ragService.uploadFile(ragTag, list);
    }

    /**
     * 查询RAG标签
     *
     * @param msg
     * @param ragTag
     * @return 响应结果
     */
    @Operation(summary = "查询RAG标签")
    @GetMapping("/queryRAGTags")
    public R queryRAGTags(@RequestParam String msg, @RequestParam String ragTag) {
        return ok(ragService.getTags(msg, ragTag));
    }

    @Resource
    private AiModelManager aiModelManager;
    /**
     * 测试RAG前置提示词优化
     */
    @Operation(summary = "测试RAG前置提示词优化")
    @GetMapping("/testRAGPrompt")
    public String testRAGPrompt(String input,@RequestParam(defaultValue = "title") String type) {

        String summaryPrompt = String.format(PromptConstant.CONTENT_SUMMARY_PROMPT, input);
        if("title".equals(type)){
            summaryPrompt = String.format(PromptConstant.TITLE_GENERATION_PROMPT,input);
        }

        String summary = aiModelManager.call(AiModelEnum.LLaMA_3_1.getModelName(),summaryPrompt);

        return summary;
    }
}
